package com.zrf.springCloudTest.filter;

import com.google.common.collect.Lists;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.zrf.springCloudTest.constant.ZuulFilterType;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author zhouruifeng
 *         Created by zhouruifeng on 2017/5/5.
 */
@Slf4j
@Component
public class AccessFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return ZuulFilterType.PRE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String token = request.getParameter("token");
        if ("123456".equals(token)) {
            log.debug("validate token success:{}", token);
            return null;
        }
        requestContext.setSendZuulResponse(false);
        requestContext.setResponseStatusCode(403);
        log.debug("validate token error:{}", token);
        return null;
    }
}
